package com.warm.mini.wework.feign;

import com.alibaba.fastjson.JSONObject;
import com.warm.mini.wework.config.RestTemplateConfig;
import com.warm.mini.wework.constant.WeworkConst;
import com.warm.mini.wework.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description api restTemplate
 * @Author warmchuang
 * @Date 2022/2/11 10:55
 */
@Slf4j
public class WeworkApiRest {

    private RestTemplate restTemplate;

    public String gettoken(String corpid, String corpsecret) {
        String accessToken = null;
        try {
            log.info("wework api-gettoken request: [corpid:{}, corpsecret:{}]", corpid, corpsecret);
            Map<String, String> param = new HashMap<>();
            param.put("corpid", corpid);
            param.put("corpsecret", corpsecret);
            JSONObject resp = restTemplate.getForEntity(WeworkConst.WEWORK_URL + WeworkConst.API_GETTOKEN + "?corpid={corpid}&corpsecret={corpsecret}", JSONObject.class, param).getBody();
            log.info("wework api-gettoken response:{}", resp);
            accessToken = resp.getString("access_token");
        } catch (Exception e) {
            log.info("wework api:gettoken error: {}", e.getMessage(), e);
        }
        return accessToken;
    }

    public JSONObject userGet(String access_token, String userid) {
        JSONObject resp = null;
        try {
            Map<String, String> param = new HashMap<>();
            param.put("access_token", access_token);
            param.put("userid", userid);
            resp = restTemplate.getForEntity(WeworkConst.WEWORK_URL + WeworkConst.API_USER_GET + "?access_token={access_token}&userid={userid}", JSONObject.class, param).getBody();
        } catch (Exception e) {
            log.info("wework api error: {}", e.getMessage(), e);
        }
        return resp;
    }

    public JSONObject getUserInfo(String access_token, String code) {
        JSONObject resp = null;
        try {
            Map<String, String> param = new HashMap<>();
            param.put("access_token", access_token);
            param.put("code", code);
            resp = restTemplate.getForEntity(WeworkConst.WEWORK_URL + WeworkConst.API_USER_GETUSERINFO + "?access_token={access_token}&code={code}", JSONObject.class, param).getBody();
        } catch (Exception e) {
            log.info("wework api error: {}", e.getMessage(), e);
        }
        return resp;
    }

    public static WeworkApiRest get() {
        return WeworkApiRest.WeworkApiEnum.INSTANCE.getInstance();
    }

    private WeworkApiRest(RestTemplate restTemplate){
        this.restTemplate = restTemplate;
    }

    private enum WeworkApiEnum {
        INSTANCE;
        private WeworkApiRest instance;

        WeworkApiEnum() {
            instance = new WeworkApiRest(SpringContextUtil.getBean(RestTemplateConfig.REST_NAME));
        }

        public WeworkApiRest getInstance() {
            return instance;
        }
    }
}
